<? include $_SERVER['DOCUMENT_ROOT'].'/fr/include/connexion.php';

if($_POST['q']=="edit"){
	$q0 = mysql_query("SELECT * FROM `produit` WHERE `pro_id` = '".$_POST['pro_id']."';");
	$r0 = mysql_fetch_array($q0);
	if(!empty($_FILES['pro_photo']['name'])) {
		$directory 	= '/img/produits/';
		$target 	= $_SERVER['DOCUMENT_ROOT'].$directory;		// Repertoire cible
		$max_size	= 200000;					// Taille max en octets du fichier
		$width_max	= 1000;						// Largeur max de l'image en pixels
		$height_max	= 1000;						// Hauteur max de l'image en pixels
		$nom_file	= $_FILES['pro_photo']['name'];
		$taille		= $_FILES['pro_photo']['size'];
		$tmp		= $_FILES['pro_photo']['tmp_name'];
		$file = substr($nom_file, -3);
		if (ereg("jpg$",$file) || ereg("JPG$",$file) || ereg("jpeg$",$file) || ereg("JPEG$",$file) || ereg("png$",$file) || ereg("PNG$",$file) || ereg("gif$",$file) || ereg("GIF$",$file)){ // On vérifie l'extension du fichier
			$infos_img = getimagesize($_FILES['pro_photo']['tmp_name']);// On récupère les dimensions du fichier
			// On vérifie les dimensions et taille de l'image
			if(($infos_img[0] <= $width_max) && ($infos_img[1] <= $height_max) && ($_FILES['pro_photo']['size'] <= $max_size)) {
				if(move_uploaded_file($_FILES['pro_photo']['tmp_name'],$target.$_FILES['pro_photo']['name'])) {
					$imgurl = $directory.$nom_file;
				} else {
					echo '<b>Problème lors de l\'upload !</b><br /><br /><b>', $_FILES['pro_photo']['error'], '</b><br /><br />';
				}
			} else {
				echo '<b>Problème dans les dimensions ou taille de l\'image !</b><br /><br />';
			}
		} else {
			echo '<b>Votre image n\'est pas au bon format !</b><br /><br />';
		}
	} else {
		$imgurl = $r0['pro_photo'];
	}
	//miniature
	if(!empty($_FILES['pro_thumb']['name'])) {
		$directory 	= '/img/produits/';
		$target 	= $_SERVER['DOCUMENT_ROOT'].$directory;		// Repertoire cible
		$max_size	= 200000;					// Taille max en octets du fichier
		$width_max	= 1000;						// Largeur max de l'image en pixels
		$height_max	= 1000;						// Hauteur max de l'image en pixels
		$nom_file	= $_FILES['pro_thumb']['name'];
		$taille		= $_FILES['pro_thumb']['size'];
		$tmp		= $_FILES['pro_thumb']['tmp_name'];
		$file = substr($nom_file, -3);
		if (ereg("jpg$",$file) || ereg("JPG$",$file) || ereg("jpeg$",$file) || ereg("JPEG$",$file) || ereg("png$",$file) || ereg("PNG$",$file) || ereg("gif$",$file) || ereg("GIF$",$file)){ // On vérifie l'extension du fichier
			$infos_img = getimagesize($_FILES['pro_thumb']['tmp_name']);// On récupère les dimensions du fichier
			// On vérifie les dimensions et taille de l'image
			if(($infos_img[0] <= $width_max) && ($infos_img[1] <= $height_max) && ($_FILES['pro_thumb']['size'] <= $max_size)) {
				if(move_uploaded_file($_FILES['pro_thumb']['tmp_name'],$target.$_FILES['pro_thumb']['name'])) {
					$thburl = $directory.$nom_file;
				} else {
					echo '<b>Problème lors de l\'upload.</b><br /><br /><b>', $_FILES['pro_thumb']['error'], '</b><br /><br />';
				}
			} else {
				echo '<b>Problème dans les dimensions ou taille de l\'image.</b><br /><br />';
			}
		} else {
			echo '<b>Votre image n\'est pas au bon format.</b><br /><br />';
		}
	} else {
		$thburl = $r0['pro_thumb'];
	}
	mysql_query("UPDATE `produit` SET
	`pro_id` = '".$_POST['pro_id']."',
	`pro_nom` = '".$_POST['pro_nom']."',
	`pro_nom_cn` = '".$_POST['pro_nom_cn']."',
	`pro_description` = '".$_POST['pro_description']."',
	`pro_constituant` = '".$_POST['pro_constituant']."',
	`pro_photo` = '".$imgurl."',
	`pro_thumb` = '".$thburl."',
	`pro_poids` = '".$_POST['pro_poids']."',
	`pro_poidsnet` = '".$_POST['pro_poidsnet']."',
	`pro_prix` = '".$_POST['pro_prix']."',
	`pro_recette` = '".$_POST['pro_recette']."',
	`pro_dispo` = '".$_POST['pro_dispo']."',
	`pro_category` = '".$_POST['pro_category']."'
	WHERE pro_id = '".$_POST['sel_id']."';");
	//
	mysql_query("DELETE FROM `asso_pro_rec` WHERE proa_id = '".$_POST['pro_id']."'");
	$i = 0;
	$values = "";
	foreach($_POST as $var => $value){
		if(ereg('^check_',$var)){
			$varname = split("_", $var);
			$recid = $varname[1];
			if($value=="on"){
				mysql_query("INSERT INTO `asso_pro_rec` (`proa_id`, `reca_id`) VALUES ('".$_POST['pro_id']."', '$recid');");
			}
		}
	}
}
if(isset($_POST['pro_id'])) $_GET['id'] = $_POST['pro_id'];
if(isset($_GET['id'])){
	$q1 = mysql_query("SELECT * FROM `produit` WHERE `pro_id` = '".$_GET['id']."';");
	$pro = mysql_fetch_array($q1);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>Herbes de Chine</title>
		<link href="/fr/css/common.css" rel="stylesheet" type="text/css" />
		<link href="/fr/css/inside.css" rel="stylesheet" type="text/css" />
		<link href="/fr/css/admin.css" rel="stylesheet" type="text/css" />
	</head>
	<body>
		﻿<div id="header">
			<table id="top">
				<tr>
					<td id="logo" rowspan="2"><a href="/fr/index.php"><img src="/img/common/header/logo.gif" alt="Herbes de Chine" /></a></td>
					<td id="connexion"></td>
				</tr>
				<tr>
					<td id="cart"></td>
				</tr>
			</table>
		</div>
		<div id="tools">
			<table>
				<tr>
					<td id="title">Modifier un produit</td>
					<? include $_SERVER['DOCUMENT_ROOT']."/fr/admin/nav.php"; ?>
				</tr>
			</table>
		</div>
		<td id="search">
		    <table id="body" class="widescreen">
				<tr class="top"><td colspan="2"></td></tr>
			    <tr class="middle"><td class="content">
					<div id="form" class="double"><form name="form" action="?" method="POST" ENCTYPE="multipart/form-data"><input type="hidden" name="q" value="edit" />
						<table class="clear">
							<tr><td colspan="2">&nbsp;</td></tr>
							<tr>
								<td width="130">Référence</td>
								<td><select name="sel_id" onchange="location='?id='+this.options[this.selectedIndex].value"><option></option>
								<?
								$q2 = mysql_query("SELECT * FROM produit");
								while ($r2 = mysql_fetch_array($q2) ){
									if($pro['pro_id']==$r2['pro_id']){
										$s = "selected";
									} else {
										$s = "";
									}
									echo "<option value='".$r2['pro_id']."' $s>".$r2['pro_id']." - ".$r2['pro_nom']."</option>";
								}
								?>
								</select></td>
							</tr>
						</table>
						<? if(isset($_GET['id'])){ ?>
						<table class="clear">
							<tr><td colspan="2" class="bar"><img src="/img/common/lightgreybar.gif" alt="" /></td></tr>
							<tr><td colspan="2"><h2>Fiche produit</h2></td></tr>
						</table>
						<div class="left">
							<table>
								<tr>
									<td width="130">Référence</td>
									<td><input type="text" name="pro_id" value="<? echo $pro['pro_id']; ?>" class="text" /></td>
								</tr>
								<tr>
									<td width="130">Nom du produit</td>
									<td><input type="text" name="pro_nom" value="<? echo $pro['pro_nom']; ?>" class="text" /></td>
								</tr>
								<tr>
									<td>Nom chinois</td>
									<td><input type="text" name="pro_nom_cn" value="<? echo $pro['pro_nom_cn']; ?>" class="text" /></td>
								</tr>
								<tr>
									<td>Description</td>
									<td><textarea type="text" name="pro_description"><? echo $pro['pro_description']; ?></textarea></td>
								</tr>
								<tr>
									<td>Constituants</td>
									<td><textarea type="text" name="pro_constituant"><? echo $pro['pro_constituant']; ?></textarea></td>
								</tr>
							</table>
						</div>
						<div class="right">
							<table>
								<tr>
									<td>Photo (<a target="_blank" href="<? echo $pro['pro_photo']; ?>">Voir</a>)</td>
									<td><input type="file" name="pro_photo" class="text" /></td>
								</tr>
								<tr>
									<td>Miniature (<a target="_blank" href="<? echo $pro['pro_thumb']; ?>">Voir</a>)</td>
									<td><input type="file" name="pro_thumb" class="text" /></td>
								</tr>
								<tr>
									<td>Poids</td>
									<td><input type="text" name="pro_poids" value="<? echo $pro['pro_poids']; ?>" class="text" /></td>
								</tr>
								<tr>
									<td>Poids à facturer</td>
									<td><input type="text" name="pro_poidsnet" value="<? echo $pro['pro_poidsnet']; ?>" class="text" /></td>
								</tr>
								<tr>
									<td>Prix</td>
									<td><input type="text" name="pro_prix" value="<? echo $pro['pro_prix']; ?>" class="text" /></td>
								</tr>
								<tr>
									<td>Disponibilité</td>
									<td><select name="pro_dispo">
										<option value="0" <? if($pro['pro_dispo']==0) echo "selected"; ?>>Non</option>
										<option value="1" <? if($pro['pro_dispo']==1) echo "selected"; ?>>Oui</option>
									</select></td>
								</tr>
								<tr>
									<td>Catégorie</td>
									<td><select name="pro_category"><option></option>
									<?
									$q2 = mysql_query("SELECT * FROM category");
									while ($r2 = mysql_fetch_array($q2) ){
										if($pro['pro_category']==$r2['cat_id']){
											$ischecked = "selected";
										} else {
											$ischecked = "";
										}
										echo "<option value='".$r2['cat_id']."' $ischecked>".$r2['cat_label']."</option>";
									}
									?>
									</select></td>
								</tr>
							</table>
						</div>
						<table class="clear">
							<tr><td colspan="2"><h2>Recettes associées</h2></td></tr>
						</table>
						<div>
							<table>
								<tr>
									<td width="130">Idée recette</td>
									<td><select name="pro_recette"><option></option>
									<?
									$q1 = mysql_query("SELECT * FROM recette");
									while ($r1 = mysql_fetch_array($q1)){
										if($pro['pro_recette']==$r1['rec_id']){
											$ischecked = "selected";
										} else {
											$ischecked = "";
										}
										echo "<option value='".$r1['rec_id']."' $ischecked>".$r1['rec_nom']."</option>";
									}
									?>
									</select></td>
								</tr>
								<tr>
									<td>Recettes associées</td>
									<td>
									<?
									$q3 = mysql_query("SELECT * FROM recette");
									while ($r3 = mysql_fetch_array($q3) ){
										$q2 = mysql_query("SELECT * FROM asso_pro_rec WHERE `proa_id` = '".$pro['pro_id']."'");
										$ischecked = "";
										while ($r2 = mysql_fetch_array($q2)){
											if($r2['reca_id']==$r3['rec_id']){
												$ischecked = "checked";
											}
										}
										echo "<input type='checkbox' name='check_".$r3['rec_id']."' id='check_".$r3['rec_id']."' $ischecked><label for='check_".$r3['rec_id']."'>".$r3['rec_nom']."</label> ";
									}
									?>
									</td>
								</tr>
								<tr><td colspan="2">&nbsp;</td></tr>
							</table>
						</div>
						<table class="clear">
							<tr><td colspan="2" class="bar"><img src="/img/common/lightgreybar.gif" alt="" /></td></tr>
							<tr>
								<td colspan="2" class="addcart aright"><a href="#" onClick="form.submit();"><img src="/img/common/enregistrer.gif" alt="Enregistrer" /></a></td>
							</tr>
						</table>
						<? } else { ?>
						<table class="clear"><tr><td colspan="2">&nbsp;</td></tr></table>
						<? } ?>
					</form></div>
				</td></tr>
				<tr class="bottom"><td colspan="2"></td></tr>
			</table>﻿
		</div>
	</body>
</html>
